package com.servicesystem.servicesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.servicesystem.servicesystem.entity.TtOrder;
import com.servicesystem.servicesystem.entity.vo.ali.CreateOrderVo;
import com.servicesystem.servicesystem.service.TtOrderService;
import com.servicesystem.servicesystem.untis.HttpRequestAll;
import com.servicesystem.servicesystem.untis.R;
import com.servicesystem.servicesystem.untis.ali.AliData;
import com.servicesystem.servicesystem.untis.ali.AliSign;
import com.servicesystem.servicesystem.untis.ali.RequestUrl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (TtOrder)表控制层
 *
 * @author makejava
 * @since 2020-09-11 11:54:58
 */
@RestController
@RequestMapping("ttOrder")
public class TtOrderController {

    private final HttpRequestAll httpRequestAll = new HttpRequestAll();

    /**
     * 服务对象
     */
    @Resource
    private TtOrderService ttOrderService;

    @PutMapping("fix-tt-order-status/{ttOrderId}")
    public R fixTtOrderStatus(@PathVariable(value = "ttOrderId") String ttOrderId) {
        QueryWrapper<TtOrder> ttOrderQueryWrapper = new QueryWrapper<>();
        ttOrderQueryWrapper.eq("id", ttOrderId);
        TtOrder ttOrder = new TtOrder();
        //ttOrder.setOrderStatus("3");
        ttOrder.setStatus("2");
        ttOrderService.update(ttOrder, ttOrderQueryWrapper);
        return R.success();
    }

    @PostMapping("add-tt-order")
    public R addTtOrder(@RequestBody TtOrder ttOrder) {
        ttOrderService.save(ttOrder);
        return R.success();
    }

    @GetMapping("get-order/{current}")
    public R getOrder(@PathVariable Long current) {
        Page<TtOrder> ttOrderPage = ttOrderService.getAll(current);
        return R.success().data(ttOrderPage);
    }

    @PutMapping("set-tt-order-over/{orderId}")
    public R setTtOrderOver(@PathVariable(value = "orderId") String orderId) {
        ttOrderService.setTtOrderOver(orderId);
        return R.success();
    }

    @PostMapping("add-ali-order")
    public R addAliOrder(@RequestBody CreateOrderVo createOrderVo) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("addressParam", createOrderVo.getAddressParam());
            params.put("cargoParamList", createOrderVo.getCargoParamList());
            params.put("message", createOrderVo.getMessage());
            params.put("outerOrderInfo", createOrderVo.getOuterOrderInfo());
            String signature = AliSign.getSignature("param2/1/com.alibaba.trade/alibaba.trade.createOrder4CybMedia/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("addressParam", createOrderVo.getAddressParam());
            objectObjectHashMap.put("cargoParamList", createOrderVo.getCargoParamList());
            objectObjectHashMap.put("message", createOrderVo.getMessage());
            objectObjectHashMap.put("outerOrderInfo", createOrderVo.getOuterOrderInfo());
            objectObjectHashMap.put("_aop_signature", signature);
            // 添加阿里的订单数据
            return httpRequestAll.doPostWithParams(RequestUrl.CREATE_ORDER, objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }

    @PostMapping("get-ali-order/{current}")
    public R getAliOrder(@PathVariable("current") Integer current) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("page", current.toString());
            String signature = AliSign.getSignature("param2/1/com.alibaba.trade/alibaba.trade.getBuyerOrderList/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("page", current.toString());
            objectObjectHashMap.put("_aop_signature", signature);
            return httpRequestAll.doPostWithParams(RequestUrl.GET_ALI_ORDER, objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }

    @PostMapping("ali-pay")
    public R aliPay(@RequestBody String orderList) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("orderIdList", orderList);
            String signature = AliSign.getSignature("param2/1/com.alibaba.trade/alibaba.alipay.url.get/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("orderIdList", orderList);
            objectObjectHashMap.put("_aop_signature", signature);
            return httpRequestAll.doPostWithParams(RequestUrl.ALI_PAY, objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("支付失败");
    }

    @PostMapping("get-address-code")
    public R getAddressCode(String address) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("addressInfo", address);
            String signature = AliSign.getSignature("param2/1/com.alibaba.trade/alibaba.trade.addresscode.parse/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("addressInfo", address);
            objectObjectHashMap.put("_aop_signature", signature);
            return httpRequestAll.doPostWithParams(RequestUrl.GET_ADDRESS_CODE, objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }
}
